#!/bin/bash

## hyphop ##

#= make install modules

D=$(realpath "$(dirname "$0")") # "


. "$D"/config.conf

echo "[i] make opkgs : $KERNEL_OUTPUT_DIR"

CMD(){
    echo "[#] $@">&2
    "$@"
}

cd $KERNEL_OUTPUT_DIR

M=$INSTALL_MOD_PATH/lib/modules/0.0.0

control(){

echo "Package: $1
Version: 0.0.0
License: GPL-2.0
Section: kernel
Architecture: aarch64_generic
Description:  kernel modules package
"
}
postinst(){
echo '#!/bin/sh
[ "${IPKG_NO_SCRIPT}" = "1" ] && exit 0
[ -x ${IPKG_INSTROOT}/lib/functions.sh ] || exit 0
. ${IPKG_INSTROOT}/lib/functions.sh
default_postinst $0 $@
'
}

prerm(){
echo '#!/bin/sh
[ -x ${IPKG_INSTROOT}/lib/functions.sh ] || exit 0
. ${IPKG_INSTROOT}/lib/functions.sh
default_prerm $0 $@
'
}

gzip=pigz

T=/tmp/kmod-opkg

while read l ; do
    case $l in
	\#*|"")
	continue
	;;
    esac
    name=
    list=
    files=
    for i in $l; do
    [ "$name" ] || {
    name=$i
    continue
    }

    list="$list
$i"
    n=${M#/}/${i//_/?}.ko
    #n=${M}/${i//_/?}.ko
    r=$(ls -1 $n)
    [ "$r" ] || {
	echo "[i] $n missed">&2
#	exit 1
    }
    files="$files
${r#/}"
    done

    list=$(echo "$list" | sort -u)
    files=$(echo "$files" | sort -u)

    k="kernel-modules-$name"
    echo "[i] $name -> $k"
    echo $list
    echo $files
#   continue

    rm -rf $T
    mkdir -p $T
    control $k > $T/control
    postinst "" > $T/postinst
    prerm "" > $T/prerm
    chmod 0777 $T/p*
    tar -C$T -czf $T/control.tar.gz control postinst prerm
    rm $T/control $T/postinst $T/prerm
    tar -cf- $files | $gzip -9 > $T/data.tar.gz

    echo "# MODULES BEGIN"
    for f in $files; do
	f=${f##*/}
	echo "${f%.ko}"
    done
    echo "# MODULES END"

    echo 2.0 > $T/debian-binary
    tar -C$T -cf- control.tar.gz data.tar.gz debian-binary | $gzip \
      > $KERNEL_OUTPUT_DIR/$k.ipk

done \
< $D/subparts.conf

